package com.lvxing.admin.jiguang;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.user.UserInfoResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Method;

/**
 * 创建用户的过程可以在极光后台完成 支持excel批量导入
 * instant message
 */
public class JMessage {

    private static String appkey = "1f78130730faa3445c91a046";
    private static String masterSecret = "7fd869abf5d811e3d3c9f5d1";

    public static void testGetUserInfo() {
//        JMessageClient client = new JMessageClient(APP_KEY, MASTER_SECRET);
    }

    @Test
    public void test() throws APIConnectionException, APIRequestException {
        System.out.println(123);
        JMessageClient client = new JMessageClient(appkey, masterSecret);
        // 示例代码 String res = client.getUserInfo("test_user");
        UserInfoResult test_user = client.getUserInfo("1234");
        System.out.println(JSON.toJSONString(test_user));
    }

    class jiguanguser{
        private String userName;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        private String password;


        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    @Test
    public void test1() {
        System.out.println(222);
//        List<jiguanguser> jiguangusers = new ArrayList<>();
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 20; i++) {
            jiguanguser jiguanguser = new jiguanguser();
            jiguanguser.setPassword("123"+i);
            jiguanguser.setUserName("123"+i);
            jsonArray.add(jiguanguser);
        }

    }

    @Test
    public void test2() {
        try {
            // 生产base64
            String s = JMessage.encodeBase64((appkey + ":" + masterSecret).getBytes());
//            System.out.println(s);
            // > POST /v3/push HTTP/1.1
            //> Authorization: Basic MWY3ODEzMDczMGZhYTM0NDVjOTFhMDQ2OjdmZDg2OWFiZjVkODExZTNkM2M5ZjVkMQ==
            // 鉴权的value
            System.out.println("Basic " + s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void test3() throws APIConnectionException, APIRequestException {
        JMessageClient client = new JMessageClient(appkey, masterSecret);
        // 获取发送的消息
        client.getUserMessages("12345", 10, "2019-07-20 00:00:00", "2019-07-25 00:00:00");
    }

    /**
     * 编码
     *
     * @param content
     * @return
     */
    public static String encode(byte[] content) {
        return new sun.misc.BASE64Encoder().encode(content);
    }

    /**
     * 解码
     *
     * @param source
     * @return
     */
    public static byte[] decode(String source) {
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            return decoder.decodeBuffer(source);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 编码
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static String encodeBase64(byte[] input) throws Exception {
        Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
        Method mainMethod = clazz.getMethod("encode", byte[].class);
        mainMethod.setAccessible(true);
        Object retObj = mainMethod.invoke(null, new Object[]{input});
        return (String) retObj;
    }

    /**
     * 解码
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static byte[] decodeBase64(String input) throws Exception {
        Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
        Method mainMethod = clazz.getMethod("decode", String.class);
        mainMethod.setAccessible(true);
        Object retObj = mainMethod.invoke(null, input);
        return (byte[]) retObj;
    }

}
